import { Order, Page, Topup, User, type PageIn, type TinyUser } from '../client'
import { makeRequest } from '../client-core'
import { Comment, CommentIn, OrderIn, TopupIn } from './models'

const request = makeRequest('/user/api/v1')

export class Users extends request('/user') {
  static retrieve() {
    return Users.$api.get<User>({ typeof: User })
  }

  static update(data: User) {
    return Users.$api.put({
      data,
      includes: ['username', 'password', 'name', 'avatar']
    })
  }
}

export class Copilots extends request('/copilot') {
  static trendings(v: string) {
    return Copilots.$api.get<User[]>({
      sub: '/trendings',
      paths: [v],
      transform: (data) => data.map((x: any) => User.inst(x))
    })
  }

  static search(v: string) {
    return Copilots.$api.get<User[]>({
      sub: '/search',
      paths: [v],
      transform: (data) => data.map((x: any) => User.inst(x))
    })
  }

  static tinylist() {
    return Copilots.$api.get<TinyUser[]>({ sub: '/tinylist' })
  }

  static list(params: PageIn) {
    return Copilots.$api.get<Page<User>>({ params, transform: (data) => Page.inst<any>(data, [User]) })
  }

  static retrieve(id: number) {
    return Copilots.$api.get<User>({ paths: [id], typeof: User })
  }
}

export class Comments extends request('/comment') {
  static list(copilot: number) {
    return Comments.$api.get<Comment[]>({
      paths: [copilot],
      transform: (data) => data.map((x: any) => Comment.inst(x))
    })
  }

  static create(data: CommentIn) {
    return Comments.$api.post({ data, includes: ['copilot', 'content'] })
  }
}

export class Topups extends request('/topup') {
  static list(params: PageIn) {
    return Topups.$api.get<Page<Topup>>({ params, transform: (data) => Page.inst<any>(data, [Topup]) })
  }

  static retrieve(id: number) {
    return Topups.$api.get<Topup>({ paths: [id], typeof: Topup })
  }

  static create(data: TopupIn) {
    return Topups.$api.post({ data })
  }
}

export class Orders extends request('/order') {
  static list(params: PageIn) {
    return Orders.$api.get<Page<Order>>({ params, transform: (data) => Page.inst<any>(data, [Order]) })
  }

  static retrieve(id: number) {
    return Orders.$api.get<Order>({ paths: [id], typeof: Order })
  }

  static create(data: OrderIn) {
    return Orders.$api.post({ data })
  }
}

export class Stars extends request('/star') {
  static list(params: PageIn) {
    return Stars.$api.get<Page<User>>({ params, transform: (data) => Page.inst<any>(data, [User]) })
  }

  static create(copilot: number) {
    return Stars.$api.post({ data: { copilot } })
  }
}
